#' 清洗样本量
#'
#' 查看样本量是否正确，不正确的话需要修改，对名字不满意也需要修改
#'
#'
#' @param dat U4_harmonise_data后的dat
#'
#' @return dat
#' @export
#'
#' @examples
#'
#' help(U4_harmonise_data)
#'
#'
#'
U4_check_samplesize<- function(dat){

  dat1 <- plyr::ddply(dat,"id.exposure",
                      function(dat) {

                        if( length( unique(dat$samplesize.exposure) )>1 ){
                          message("暴露",dat$exposure[1],"有多个不同的样本量，只选择最大的样本量进行后续计算")
                        }

                        if( ( "ncase.exposure" %in% colnames(dat)) & ( "ncontrol.exposure" %in% colnames(dat) ) ){

                          dat$ncase.exposure<-  as.numeric(dat$ncase.exposure)
                          dat$ncontrol.exposure<-  as.numeric(dat$ncontrol.exposure)

                          samplesize.exposure <- as.numeric(  max(dat$samplesize.exposure) )

                          if( all(!is.na(dat$ncase.exposure))  & all( !is.na(dat$ncontrol.exposure)) ){

                            dat$type.exposure<- 'Binary'
                            dat$ncase.exposure<-dat$ncase.exposure[which(dat$samplesize.exposure == samplesize.exposure)[1] ]
                            dat$ncontrol.exposure<-dat$ncontrol.exposure[which(dat$samplesize.exposure == samplesize.exposure)[1] ]
                            dat$samplesize.exposure <- as.numeric(  max(dat$samplesize.exposure) )

                          }else{
                            dat$type.exposure<-"Continuous"
                            dat$samplesize.exposure <- as.numeric(  max(dat$samplesize.exposure) )
                          }

                        }else{
                          dat$samplesize.exposure <- as.numeric(  max(dat$samplesize.exposure) )
                          dat$type.exposure<-"Continuous"
                        }

                        return(dat)

                      } )

  for (i in 1:nrow(dat1) ) {
    dat1$description_exposure[i] <- paste0( "暴露为", dat1$exposure[i], "。总样本量为",dat1$samplesize.exposure[i],"。推断为",ifelse( dat1$type.exposure[i]=="Continuous", "连续变量"  , paste0("二分类变量,病例样本量为",dat1$ncase.exposure[i],",对照样本量为",dat1$ncontrol.exposure[i] )  ) )
  }

  message( "请查看下面暴露的描述是否有错误,如果有，请用help(U4_harmonise_data)查看如何修改样本量等信息。\n" )
  print( unique(dat1$description_exposure) )

  dat2 <- plyr::ddply(dat1,c("id.outcome"),
                      function(dat) {

                        if( length( unique(dat$samplesize.outcome) )>1 ){
                          message("结局",dat$outcome[1],"有多个不同的样本量，只选择最大的样本量进行后续计算")
                        }

                        if( ( "ncase.outcome" %in% colnames(dat)) & ( "ncontrol.outcome" %in% colnames(dat) ) ){

                          dat$ncase.outcome<-  as.numeric(dat$ncase.outcome)
                          dat$ncontrol.outcome<-  as.numeric(dat$ncontrol.outcome)

                          samplesize.outcome <- as.numeric(  max(dat$samplesize.outcome) )

                          if( all(!is.na(dat$ncase.outcome))  & all( !is.na(dat$ncontrol.outcome)) ){

                            dat$type.outcome<- 'Binary'
                            dat$ncase.outcome<-dat$ncase.outcome[which(dat$samplesize.outcome == samplesize.outcome)[1] ]
                            dat$ncontrol.outcome<-dat$ncontrol.outcome[which(dat$samplesize.outcome == samplesize.outcome)[1] ]
                            dat$samplesize.outcome <- as.numeric(  max(dat$samplesize.outcome) )

                          }else{
                            dat$type.outcome<-"Continuous"
                            dat$samplesize.outcome <- as.numeric(  max(dat$samplesize.outcome) )
                          }

                        }else{
                          dat$samplesize.outcome <- as.numeric(  max(dat$samplesize.outcome) )
                          dat$type.outcome<-"Continuous"
                        }
                        return(dat)
                      })


  for (i in 1:nrow(dat2) ) {
    dat2$description_outcome[i] <- paste0( "结局为", dat2$outcome[i], "。总样本量为",dat2$samplesize.outcome[i],"。推断为",ifelse( dat2$type.outcome[i]=="Continuous", "连续变量", paste0("二分类变量,病例样本量为",dat2$ncase.outcome[i],",对照样本量为",dat2$ncontrol.outcome[i] )  ) )
  }

  message( "\n\n请查看下面结局的描述是否有错误,如果有，请用help(U4_harmonise_data)查看如何修改结局名字，样本量等信息。\n" )
  print( unique(dat2$description_outcome) )

  return( dat2 )

}
